<!DOCTYPE html>
<html>
  <head>
    <title>
      mediastreamaudiosourcenode.html
    </title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
    <script src="../resources/audit-util.js"></script>
    <script src="../resources/audit.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let audit = Audit.createTaskRunner();

      audit.define(
          {
            label: 'test',
            description: 'Basic tests for MediaStreamAudioSourceNode API'
          },
          (task, should) => {
            should(
                () => {navigator.webkitGetUserMedia(
                    {audio: true},
                    (stream) => {
                      gotStream(stream, should);
                      task.done();
                    },
                    () => {
                      should(false, 'Stream generation')
                          .message('succeeded', 'failed');
                      task.done();
                    })},
                'getUserMedia()')
                .notThrow();
          });

      audit.run();

      function gotStream(stream, should) {
        should(true, '{audio:true} generated stream').message('correctly', '');

        let context = new AudioContext();

        // Create an AudioNode from the stream.
        let mediaStreamSource = context.createMediaStreamSource(stream);

        // Check number of inputs and outputs.
        should(
            mediaStreamSource.numberOfInputs,
            'mediaStreamSource.numberOfInputs')
            .beEqualTo(0);
        should(
            mediaStreamSource.numberOfOutputs,
            'mediaStreamSource.numberOfOutputs')
            .beEqualTo(1);

        // Verify the same object is returned
        let element = mediaStreamSource.mediaStream;
        should(
            element instanceof MediaStream,
            'mediaStreamSource.mediaStream instanceof MediaStream')
            .beTrue();
        should(
            mediaStreamSource.mediaStream === element,
            'mediaStreamSource.mediaStream is same object')
            .beTrue();

        // Try calling connect() method with illegal values.
        should(
            () => mediaStreamSource.connect(0, 0, 0),
            'mediaStreamSource.connect(0, 0, 0)')
            .throw();
        should(
            () => mediaStreamSource.connect(context.destination, 5, 0),
            'mediaStreamSource.connect(context.destination, 5, 0)')
            .throw();
        should(
            () => mediaStreamSource.connect(context.destination, 0, 5),
            'mediaStreamSource.connect(context.destination, 0, 5)')
            .throw();

        // Try calling connect() with proper values.
        should(
            () => mediaStreamSource.connect(context.destination, 0, 0),
            'mediaStreamSource.connect(context.destination, 0, 0)')
            .notThrow();
      }
    </script>
  </body>
</html>
